/*     */ package com.zimbra.cs.account.ldap.upgrade;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.cs.account.Entry;
/*     */ import com.zimbra.cs.account.Entry.EntryType;
/*     */ import com.zimbra.cs.account.ldap.LdapHelper;
/*     */ import com.zimbra.cs.account.ldap.LdapProv;
/*     */ import com.zimbra.cs.account.ldap.entry.LdapEntry;
/*     */ import com.zimbra.cs.ldap.LdapClient;
/*     */ import com.zimbra.cs.ldap.LdapException;
/*     */ import com.zimbra.cs.ldap.LdapServerType;
/*     */ import com.zimbra.cs.ldap.LdapUsage;
/*     */ import com.zimbra.cs.ldap.ZAttributes;
/*     */ import com.zimbra.cs.ldap.ZLdapContext;
/*     */ import com.zimbra.cs.ldap.ZLdapFilter;
/*     */ import com.zimbra.cs.ldap.ZLdapFilterFactory;
/*     */ import com.zimbra.cs.ldap.ZLdapFilterFactory.FilterId;
/*     */ import com.zimbra.cs.ldap.ZMutableEntry;
/*     */ import java.util.Map;
/*     */ import java.util.Map.Entry;
/*     */ import org.apache.commons.cli.CommandLine;
/*     */ import org.apache.commons.cli.HelpFormatter;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public abstract class UpgradeOp
/*     */ {
/*     */   LdapUpgradePrinter printer;
/*     */   protected boolean verbose;
/*     */   protected String bug;
/*     */   protected LdapProv prov;
/*     */   
/*     */   abstract void doUpgrade()
/*     */     throws ServiceException;
/*     */   
/*     */   boolean parseCommandLine(CommandLine cl)
/*     */   {
/*  50 */     return true;
/*     */   }
/*     */   
/*     */ 
/*     */   void usage(HelpFormatter helpFormatter) {}
/*     */   
/*     */   void setPrinter(LdapUpgradePrinter printer)
/*     */   {
/*  58 */     this.printer = printer;
/*     */   }
/*     */   
/*     */   void setVerbose(boolean verbose) {
/*  62 */     this.verbose = verbose;
/*     */   }
/*     */   
/*     */   void setLdapProv(LdapProv prov) {
/*  66 */     this.prov = prov;
/*     */   }
/*     */   
/*     */   String getBug() {
/*  70 */     return this.bug;
/*     */   }
/*     */   
/*     */   void setBug(String bug) {
/*  74 */     this.bug = bug;
/*     */   }
/*     */   
/*     */   void describe() {
/*  78 */     Description desc = getDescription();
/*  79 */     if (desc == null) {
/*  80 */       this.printer.format("\nBug %s", new Object[] { getBug() + ": description not available.\n\n" });
/*     */     } else {
/*  82 */       desc.describe();
/*     */     }
/*     */   }
/*     */   
/*     */   Description getDescription() {
/*  87 */     return null;
/*     */   }
/*     */   
/*     */   private void printModAttrs(String dn, Map<String, Object> attrs) {
/*  91 */     this.printer.println();
/*  92 */     this.printer.println("Modifying " + dn);
/*  93 */     printModAttrs(attrs);
/*     */   }
/*     */   
/*     */   private void printModAttrs(Entry entry, Map<String, Object> attrs) {
/*  97 */     if (!attrs.isEmpty()) {
/*  98 */       this.printer.println();
/*  99 */       this.printer.println("Modifying " + entry.getEntryType().name() + " " + entry.getLabel());
/* 100 */       printModAttrs(attrs);
/*     */     }
/*     */   }
/*     */   
/*     */   private void printModAttrs(Map<String, Object> attrs) {
/* 105 */     for (Map.Entry<String, Object> attr : attrs.entrySet()) {
/* 106 */       String key = (String)attr.getKey();
/* 107 */       Object value = attr.getValue();
/*     */       
/* 109 */       if ((value instanceof String)) {
/* 110 */         this.printer.println("  " + key + ": " + (String)value);
/* 111 */       } else if ((value instanceof String[])) {
/* 112 */         for (String v : (String[])value) {
/* 113 */           this.printer.println("  " + key + ": " + v);
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   protected void replaceAttrs(ZLdapContext initZlc, String dn, ZMutableEntry entry) throws ServiceException
/*     */   {
/* 121 */     Map<String, Object> attrs = entry.getAttributes().getAttrs();
/* 122 */     if (attrs.size() == 0) {
/* 123 */       return;
/*     */     }
/*     */     
/* 126 */     printModAttrs(dn, attrs);
/*     */     
/* 128 */     ZLdapContext zlc = initZlc;
/*     */     try {
/* 130 */       if (zlc == null) {
/* 131 */         zlc = LdapClient.getContext(LdapServerType.MASTER, LdapUsage.UPGRADE);
/*     */       }
/* 133 */       zlc.replaceAttributes(dn, entry.getAttributes());
/*     */     }
/*     */     finally {
/* 136 */       if (initZlc == null) {
/* 137 */         LdapClient.closeContext(zlc);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   protected void modifyAttrs(ZLdapContext initZlc, Entry entry, Map<String, Object> attrs) throws ServiceException
/*     */   {
/* 144 */     if (attrs.isEmpty()) {
/* 145 */       return;
/*     */     }
/*     */     
/* 148 */     printModAttrs(entry, attrs);
/*     */     
/* 150 */     ZLdapContext zlc = initZlc;
/*     */     try {
/* 152 */       if (zlc == null) {
/* 153 */         zlc = LdapClient.getContext(LdapServerType.MASTER, LdapUsage.UPGRADE);
/*     */       }
/* 155 */       this.prov.getHelper().modifyAttrs(zlc, ((LdapEntry)entry).getDN(), attrs, entry);
/*     */     } finally {
/* 157 */       if (initZlc == null) {
/* 158 */         LdapClient.closeContext(zlc);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   protected void modifyAttrs(Entry entry, Map<String, Object> attrs) throws ServiceException
/*     */   {
/* 165 */     if (attrs.isEmpty()) {
/* 166 */       return;
/*     */     }
/*     */     
/* 169 */     printModAttrs(entry, attrs);
/* 170 */     this.prov.modifyAttrs(entry, attrs);
/*     */   }
/*     */   
/*     */   protected ZLdapFilter getFilter(String filter) throws LdapException {
/* 174 */     return ZLdapFilterFactory.getInstance().fromFilterString(ZLdapFilterFactory.FilterId.LDAP_UPGRADE, filter);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/account/ldap/upgrade/UpgradeOp.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */